package com.captjack.bigdata.hadoop.hbase;

import com.captjack.bigdata.hadoop.hbase.nature.HBaseConnection;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Capt Jack
 * @date 2017/12/12
 */
public class HBaseTest {

    /**
     * 本地host配置
     * <p>
     * # 大数据配置（测试环境）
     * 192.168.104.59 CDH59
     * <p>
     * 192.168.104.61 CDH61
     * <p>
     * 192.168.104.63 CDH63
     * <p>
     * # 大数据配置（研发环境）
     * 192.168.110.191 host191
     * <p>
     * 192.168.110.192 host192
     * <p>
     * 192.168.110.194 host194
     * <p>
     * 192.168.110.195 host195
     * <p>
     * 192.168.110.197 host197
     * <p>
     * 192.168.100.223 host100-223
     */

    private static final long vid = 1282030620745398098L;

    private static final long startTime = 1514120832619L;

    private static final long endTime = 1514639232619L;

    private static final Logger logger = LoggerFactory.getLogger(HBaseTest.class);

    public static void main(String[] args) {
        Connection connection = null;
        try {
            connection = HBaseConnection.getConnection();
            Table table = connection.getTable(TableName.valueOf(""));
            System.out.println(table.getName());
            Scan scan = new Scan();
            scan.setTimeRange(1468574692919L, 1468574772038L);
            ResultScanner resultScanner = table.getScanner(scan);
            for (Result result : resultScanner) {
                String trackInfo = Bytes.toString(result.getValue(Bytes.toBytes("Cf"), Bytes.toBytes("info")));
                if (!"".equals(trackInfo)) {
                    System.out.println(trackInfo);
                }
            }
//            System.out.println(resultScanner.iterator().next().size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main1(String[] args) {
        // 连接
        Connection connection = null;
        try {
            connection = HBaseConnection.getConnection();
            Table table = connection.getTable(TableName.valueOf("user_info"));
            System.out.println(table.getName().getNameAsString());

            // 根据指定规则查询
            ResultScanner resultScanner = table.getScanner(new Scan());
//            ResultScanner resultScanner = table.getScanner(new Scan());

//            Result result = table.get(new Get("4910daf376f5fa25_1468830655487_1468830977000".getBytes()));
//            for (KeyValue keyValue : result.raw()) {
//                System.out.println("列：" + new String(keyValue.getFamily())
//                        + "====值:" + new String(keyValue.getValue()));
//            }
//            System.out.println(resultScanner.next().getValue(Bytes.toBytes("Cf"), Bytes.toBytes("info")).toString());
            for (Result rs : resultScanner) {
                String trackInfo = Bytes.toString(rs.getValue(Bytes.toBytes("Cf"), Bytes.toBytes("info")));
                if (!"".equals(trackInfo)) {
                    System.out.println(trackInfo);
                } else {
                    System.out.println("读取错误！");
                }
            }
            resultScanner.close();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("", e);
        } finally {
            if (connection != null) {
                HBaseConnection.closeConnection(connection);
            }
        }
    }

}
